//
//  MXHomeCoupleFestivalVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/18.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 七夕特别专题
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXHomeCoupleFestivalVC.h"
#import "MXHomeContentCollectionViewCell.h"
#import "AFNetworkHandler.h"
#import "MXHomeContentModel.h"
#import "MXHomeItemsDetailVC.h"
#import "MJRefresh.h"
@interface MXHomeCoupleFestivalVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *itemArray;
@end

@implementation MXHomeCoupleFestivalVC
- (void)dealloc
{
    [_titleString release];
    [_itemArray release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    
    // 标题Label
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.text = @"";
        titleLabel.userInteractionEnabled = YES;
        [self.view addSubview:titleLabel];
        [self.view addSubview:titleLabel];
        
        UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        backButton.frame = CGRectMake(10, 22, 30, 30);
        [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
        [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
        [titleLabel addSubview:backButton];
        
        UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        titleWord.center = titleLabel.center;
        titleWord.text = self.titleString;
        titleWord.textAlignment = NSTextAlignmentCenter;
        titleWord.font = [UIFont systemFontOfSize:16];
        [titleWord setTextColor:[UIColor blueColor]];
        [titleLabel addSubview:titleWord];
    }
    
    // 全局数组初始化
    
    self.itemArray = [NSMutableArray array];
    
    [self GetData];
    // Do any additional setup after loading the view.
}



#pragma mark - 创建集视图
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
    flowLayout.itemSize = CGSizeMake(170, 240);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 , 10 , 10, 10);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT  - 64) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXHomeContentCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
/************************* 解析数据部分 **************************/
- (void)GetData
{
    NSString *washPath = @"http://api.nanyibang.com/campaign?campaignId=0&campaignType=qinglv&hkm_sign=c361a320e99d60cc3052ee23aee91710&member_id=15243&member_type=member&page=1&random_key=61464&version=1.2.7";
    [AFNetworkHandler GETwithAFNByURL:washPath completion:^(id result) {
//        NSLog(@"%@", result);
        NSArray *dataArr = [[result objectForKey:@"data"] objectForKey:@"itemDetail"];
        for (NSDictionary *washDict in dataArr) {
            MXHomeContentModel *model = [[[MXHomeContentModel alloc] initWithDic:washDict] autorelease];
            [self.itemArray addObject:model];
        }
        [self creatCollectionView];
    }];
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArray.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell getInfo:self.itemArray[indexPath.row]];
    
    cell.layer.cornerRadius = 8;
    
    cell.layer.masksToBounds = YES;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeItemsDetailVC *ItemVC = [[[MXHomeItemsDetailVC alloc] init] autorelease];
    MXHomeContentModel *model = self.itemArray[indexPath.row];
    ItemVC.tempNum = model._id;
    [self presentViewController:ItemVC animated:YES completion:^{
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
